package com.asteros.common.util.io;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.text.StringCharacterIterator;

public final class IOUtil
{
  private static final String DEFAULT_CHARACTER_SET = "Cp1251";

  public static void saveFile(byte[] buf, String fName)
    throws IOException
  {
    FileOutputStream fstream = null;
    try {
      fstream = new FileOutputStream(fName);
      fstream.write(buf);
      fstream.flush();
    } finally {
      try {
        if (fstream != null) fstream.close(); 
      }
      catch (IOException exc)
      {
      }
    }
  }

  public static String readFile(InputStream input, String charSet) throws IOException
  {
    String data = null;
    BufferedReader bReader = null;
    try {
      bReader = new BufferedReader(new InputStreamReader(input, charSet));
      StringBuffer sBuf = new StringBuffer();
      char[] cbuf = new char[1024];
      int len = 0;
      while ((len = bReader.read(cbuf, 0, cbuf.length)) != -1) {
        sBuf.append(cbuf, 0, len);
      }
      data = sBuf.toString();
    } finally {
      try {
        bReader.close();
      }
      catch (IOException exc) {
      }
    }
    return data;
  }

  public static String readFile(InputStream input) throws IOException
  {
    String data = null;
    BufferedReader bReader = null;
    try {
      bReader = new BufferedReader(new InputStreamReader(input, "Cp1251"));
      StringBuffer sBuf = new StringBuffer();
      char[] cbuf = new char[1024];
      int len = 0;
      while ((len = bReader.read(cbuf, 0, cbuf.length)) != -1) {
        sBuf.append(cbuf, 0, len);
      }
      data = sBuf.toString();
    } finally {
      try {
        bReader.close();
      }
      catch (IOException exc) {
      }
    }
    return data;
  }

  public static byte[] readFully(InputStream in) throws IOException
  {
    ByteArrayOutputStream out = null;
    byte[] buf = new byte[1024];
    try {
      out = new ByteArrayOutputStream(1024);
      int len = 0;
      while ((len = in.read(buf)) != -1) {
        out.write(buf, 0, len);
      }
      out.flush();
      byte[] arrayOfByte1 = out.toByteArray();
      return arrayOfByte1;
    }
    finally
    {
      try
      {
        if (in != null) in.close();
        if (out != null) out.close();  } catch (Exception exc) {
      }
    }
//    throw localObject;
  }

  public static String quote(String string)
  {
    if ((string == null) || (string.length() == 0)) return "\"\"";

    char c = '\000';

    int len = string.length();
    StringBuffer sb = new StringBuffer(len + 4);

    sb.append('"');
    for (int i = 0; i < len; i++) {
      char b = c;
      c = string.charAt(i);
      switch (c) {
      case '"':
      case '\\':
        sb.append('\\');
        sb.append(c);
        break;
      case '/':
        if (b == '<') {
          sb.append('\\');
        }
        sb.append(c);
        break;
      case '\b':
        sb.append("\\b");
        break;
      case '\t':
        sb.append("\\t");
        break;
      case '\n':
        sb.append("\\n");
        break;
      case '\f':
        sb.append("\\f");
        break;
      case '\r':
        sb.append("\\r");
        break;
      default:
        if (c < ' ') {
          String t = "000" + Integer.toHexString(c);
          sb.append("\\u" + t.substring(t.length() - 4));
        } else {
          sb.append(c);
        }
      }
    }
    sb.append('"');
    return sb.toString();
  }

  public static String forXml(String text) throws IOException
  {
    if ((text == null) || ((text != null) && (text.length() == 0))) return "";

    StringWriter out = new StringWriter(text.length());
    int start = 0; int last = 0;
    char[] data = text.toCharArray();
    while (last < data.length) {
      char c = data[last];

      if (c == '<') {
        out.write(data, start, last - start);
        start = last + 1;
        out.write("&lt;");
      } else if (c == '>') {
        out.write(data, start, last - start);
        start = last + 1;
        out.write("&gt;");
      } else if (c == '&') {
        out.write(data, start, last - start);
        start = last + 1;
        out.write("&amp;");
      }
      last++;
    }
    out.write(data, start, last - start);
    return out.toString();
  }

  public static String forHTML(String aText)
  {
    if ((aText == null) || ((aText != null) && (aText.length() == 0))) return "";

    StringBuffer result = new StringBuffer(aText.length());
    StringCharacterIterator iterator = new StringCharacterIterator(aText);
    char character = iterator.current();
    while (character != 65535) {
      if (character == '<')
        result.append("&lt;");
      else if (character == '>')
        result.append("&gt;");
      else if (character == '&')
        result.append("&amp;");
      else if (character == '"')
        result.append("&quot;");
      else if (character == '\'')
        result.append("&#039;");
      else if (character == '(')
        result.append("&#040;");
      else if (character == ')')
        result.append("&#041;");
      else if (character == '#')
        result.append("&#035;");
      else if (character == '%')
        result.append("&#037;");
      else if (character == ';')
        result.append("&#059;");
      else if (character == '+')
        result.append("&#043;");
      else if (character == '-') {
        result.append("&#045;");
      }
      else
      {
        result.append(character);
      }
      character = iterator.next();
    }
    return result.toString();
  }

  public static String quoteSpecial1(String string)
  {
    if ((string == null) || (string.length() == 0)) return "";
    return string.replaceAll("\"", "'");
  }

  public static String quoteSpecial(String string)
  {
    if ((string == null) || (string.length() == 0)) return "";
    return string.replaceAll("\"", "&quot;");
  }
}

/* Location:           C:\Users\Kirill\Desktop\esign\esign\esign.jar
 * Qualified Name:     com.topsbi.common.util.io.IOUtil
 * JD-Core Version:    0.6.0
 */